﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using IFTRUE.BLL.OperationsDBSrv;

namespace IFTRUE.MongoWin
{
    public partial class CreateServer : Form
    {
        ServerProcess spBll = new ServerProcess();
        public CreateServer()
        {
            InitializeComponent();
        }

        private void btnLoadMGDBPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txtMGDBPath.Text = path.SelectedPath;
        }

        private void btnDBPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txtDBPath.Text = path.SelectedPath;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (this.txtServiceName.Text == "")
            {
                MessageBox.Show("请输入服务名称");
                return;
            }
            if (this.txtPort.Text == "")
            {
                MessageBox.Show("请输入端口号");
                return;
            }
            if (this.txtMGDBPath.Text == "")
            {
                MessageBox.Show("请选择MongoDB安装目录");
                return;
            }
            if (this.txtDBPath.Text == "")
            {
                MessageBox.Show("请选择数据库存放目录");
                return;
            }
            if (!File.Exists(this.txtMGDBPath.Text+"/mongo.exe"))
            {
                MessageBox.Show("选择的MongoDB安装目录不正确");
                return;
            }

            //拼接并执行创建服务的命令
            spBll.RunCreateServerCmd(this.txtMGDBPath.Text, this.txtDBPath.Text, this.txtServiceName.Text, this.txtPort.Text, "OutFile");
            
            if (!File.Exists(this.txtMGDBPath.Text + "\\OutFile.txt"))
            {
                txtResult.Text = "";
                MessageBox.Show("创建失败");
            }
            else
            {
                FileStream fs = new FileStream(this.txtMGDBPath.Text + "\\OutFile.txt", FileMode.Open, FileAccess.Read, FileShare.None);
                StreamReader sr = new StreamReader(fs);
                string conString = sr.ReadToEnd();
                txtResult.Text = conString;
                fs.Close();
                File.Delete(this.txtMGDBPath.Text + "\\OutFile.txt");
                MessageBox.Show("创建完成");
            }      
        }

        
    }
}
